<template>
  <view class="pay">
    <!-- 1 收货地址 -->
    <view class="address u-p-20">
      <view class="address-btn u-flex u-row-center" v-if="!address">
        <u-button
          shape="circle"
          type="error"
          plain
          size="medium"
          @click="handlerGetAddress"
          :custom-style="{ backgroundColor: '#fff!important' }"
          >获取收货地址</u-button
        >
      </view>

      <view class="address-detail" v-else>
        <view class="address-detail">{{ detail }}</view>
        <view class="address-user">{{
          address.userName + address.telNumber
        }}</view>
      </view>
    </view>

    <u-line color="#eee"></u-line>

    <!-- 2 已选商品 -->
    <view class="goods-list u-p-15">
      <view class="goods-list-title">已选商品</view>
      <view
        class="goods-list-content"
        v-for="item in checkedList"
        :key="item.goods_id"
      >
        <view class="cart-item">
          <!-- 2 图片 -->
          <view class="goods-img">
            <u-image
              width="191rpx"
              height="191rpx"
              :src="item.goods_small_logo"
            ></u-image>
          </view>
          <!-- 3 信息 -->
          <view class="goods-info">
            <!-- 名称 -->
            <view class="goods-name u-line-2">{{ item.goods_name }}</view>
            <!-- 价格和数量 -->
            <view class="goods-price-num">
              <view class="goods-price">￥{{ item.goods_price }}</view>
              <view class="goods-num-tool">
                <view class="goods-vnum">x{{ item.num }}</view>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>

    <!-- 2 统计 -->
    <view class="statistics">
      <view class="all-price">
        合计: <text>￥{{ addCount }}</text>
      </view>
      <view class="all-count">
        <u-button type="error" shape="circle" @click="goPay"
          >去支付({{ addNum }})</u-button
        >
      </view>
    </view>
  </view>
</template>

<script>
import { mapGetters, mapActions, mapState } from "vuex";
export default {
  data() {
    return {
      address: null,
    };
  },
  methods: {
    ...mapActions("user", ["getToken"]),
    // 获取地址
    async handlerGetAddress() {
      const [err, res] = await uni.chooseAddress();
      if (err) {
        console.log(err);
        return;
      }
      this.address = res;
      console.log("address", this.address);
    },
    // 去支付
    async goPay() {
      //如果地址为空，不跳转
      if (!this.address) {
        uni.showToast({
          title: "您还未选择收货地址",
          icon: "none",
        });
        return;
      }
      if (!this.token) {
        this.getToken();
      }

      // 2 获取商品编号,创建订单
      const goodsParams = {
        order_price: this.addCount,
        consignee_addr: this.detail,
        goods: this.checkedList.map((item) => {
          return {
            goods_id: item.goods_id,
            goods_number: item.num,
            goods_price: item.goods_price,
          };
        }),
      };
      //   console.log(goodsParams);
      const res4 = await this.$u.post("/my/orders/create", goodsParams);
      console.log(res4);

      //   3 获取支付参数
      const res5 = await this.$u.post("/my/orders/req_unifiedorder", {
        order_number: res4.order_number,
      });
      //   console.log(res5);
      const { pay } = res5;
      console.log("pay", pay);

      //   调用微信小程序支付功能
      const [err, res6] = await uni.requestPayment(pay);
      console.log(res6);
    },
  },
  computed: {
    ...mapGetters("cart", ["checkedList", "addCount", "addNum"]),
    ...mapState("user", ["token"]),
    // 合并地址
    detail() {
      // 空对象，先判断再执行
      if (!this.address) {
        return;
      }
      const { provinceName, cityName, countyName, detailInfo } = this.address;
      return provinceName + cityName + countyName + detailInfo;
    },
  },
};
</script>

<style lang="scss">
.pay {
  padding-bottom: 111rpx;
}
.cart-item {
  display: flex;
  padding: 10rpx;
  border-bottom: 1rpx solid #ccc;

  .goods-img {
    flex: 2;

    display: flex;
    align-items: center;
    justify-content: center;
  }

  .goods-info {
    color: #666;
    flex: 3;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    .goods-name {
    }

    .goods-price-num {
      display: flex;
      justify-content: space-between;
      .goods-price {
        color: $theme-color;
      }

      .goods-num-tool {
        width: 140rpx;
        height: 40rpx;

        display: flex;
        justify-content: flex-end;

        .goods-vnum {
        }
      }
    }
  }
}
.statistics {
  position: fixed;
  background-color: #fff;
  bottom: 0;
  left: 0;
  width: 100%;
  z-index: 100;
  border-top: 1rpx solid #ccc;
  display: flex;
  align-items: center;
  padding: 15rpx;
  .all-price {
    flex: 1;
    color: #666;
    text {
    }
  }

  .all-count {
  }
}
</style>